package com.aternity.vbd.collect;

import com.google.common.io.Resources;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Daniel Orr
 */
@Component
public class ResourceManager {

    private File sqlsDir = new File("sqls");

    public ResourceManager() {
        final String sqlsDirProp = System.getProperty("sqlDir");
        if (sqlsDirProp != null) {
            sqlsDir = new File(sqlsDirProp);
        }
    }

    public void setSqlsDir(File sqlsDir) {
        this.sqlsDir = sqlsDir;
    }

    public String getCreateTablesScript() {
        final String scriptName = "create_tables.sql";
        final String contents = getScriptFileContents(scriptName);
////        Pattern pattern = Pattern.compile(".*?create table\\s+(\\w+)\\s+\\(.*?", Pattern.CASE_INSENSITIVE);
//        Pattern pattern = Pattern.compile(".*?(--.*?$).*?", Pattern.CASE_INSENSITIVE);
//        Pattern regex = Pattern.compile("(?:/\\*[^;]*?\\*/)|(?:--[^;]*?$)", Pattern.DOTALL | Pattern.MULTILINE);
//        Pattern p = Pattern.compile("('(''|[^'])*')|[\\t\\r\\n]|(--[^\\r\\n]*)|(/\\*[\\w\\W]*?(?=\\*/)\\*/)");
//        Matcher matcher = p.matcher(contents);
//
//        if (matcher.find()) {
//            final String tableName = matcher.group(1);
//            System.out.println(tableName);
//        }


        return contents;
    }

    public static void main(String[] args) {
        ResourceManager rm = new ResourceManager();
        rm.setSqlsDir(new File("X:\\j\\dnl-pg\\atn\\vbd\\vbd-core\\sqls"));
        final String script = rm.getCreateTablesScript();

        String sql = "       select contact_id, -- a comment \r\n"
                + "              company_id \n"
                + "-- another comment\r\n"
                + "from contacts\n"
                + "where contact_id = ?";
        sql = script.replaceAll("(--.*)?(\r)?\n", " ").replaceAll("\\s+", " ");
        System.out.println(sql);
    }

    public String getCreateTriggersAndProceduresScript() {
        final String scriptName = "create_triggers_and_procedures.sql";
        return getScriptFileContents(scriptName);
    }

    public String getDropAllScript() {
        final String scriptName = "delete_tables.sql";
        return getScriptFileContents(scriptName);
    }

    public String getDropJobScript() {
        final String scriptName = "drop_job.sql";
        return getScriptFileContents(scriptName);
    }

    public String getRegisterJobScript() {
        final String scriptName = "register_job.sql";
        return getScriptFileContents(scriptName);
    }

    private String getScriptFileContents(String scriptName) {
        return getScriptFileContents(scriptName, false);
    }

    private String getScriptFileContents(String scriptName, boolean removeComments) {
        File scriptFile = new File(sqlsDir, scriptName);
        if (!scriptFile.exists()) {
            return null;
        }
        try {
            String contents = Resources.toString(scriptFile.toURI().toURL(), Charset.forName("UTF-8"));
            if(removeComments) {
                contents = contents.replaceAll("(--.*)?(\r)?\n", " ").replaceAll("\\s+", " ");
            }
            return contents;
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }
}
